var chargeId = null
var offsetH = (heightClient)*0.02;
$(function () {
    layui.use(['table', 'form'], function () {
        var table = layui.table,
            form = layui.form;
        form.verify({
            payRate: [/^100$|^(\d|[1-9]\d)(\.\d+)*$/, '费率必须是0~100的数']
        });
        var data = [
            {type: 'numbers', title: '序号'},
            {field: 'chargeName', title: '手续费名称', sort: false, align: 'center'},
            {field: 'payRate', title: '比率(%)', sort: false, align: 'center'},
            {
                field: 'payType', title: '支付方式', sort: false, align: 'center', templet: function (d) {
                    if (d["payType"] == 1) return '<span class="font-zfb">支付宝</span>';
                    else if (d["payType"] == 2) return '<span class="font-wx">微信</span>';
                }
            },
            {
                field: 'ratePayer', title: '费率承担者', sort: false, align: 'center', templet: function (d) {
                    if (d["ratePayer"] == 1) return '<span class="font-red">商户</span>'
                    else if (d["ratePayer"] == 0) return '<span class="font-cyan">市场</span>'
                    else return ''
                }
            },
            {
                field: 'serviceChargeStatus', title: '状态', sort: false, align: 'center', templet: function (d) {
                    if (d["serviceChargeStatus"] == 1) return '<span class="layui-badge layui-bg-green">有效</span>'
                    else if (d["serviceChargeStatus"] == 0) return '<span class="layui-badge layui-bg-cyan">无效</span>'
                    else return ''
                }
            },
            {title: '操作', sort: false, align: 'center', toolbar: '#barDemo'}
        ];
        table.render(
            renderTable("#chargeList", 'serviceCharge/page', data)
        );

        table.on('tool(chargeList)', function (obj) {
            var data = obj.data;
            chargeId = data['serviceChargeId']
            if (obj.event === "edit") {
                layer.open({
                    type: 1,
                    title: "编辑手续费信息",
                    btnAlign: "c",
                    offset: [offsetH],
                    area: ['600px', '430px'],
                    content: $("#chargeDiv"),
                    success: function () {
                        form.val('editForm', {
                            chargeName: data.chargeName,
                            locationName: data.locationName,
                            payRate: data.payRate,
                            payType: data.payType,
                            ratePayer: data.ratePayer,
                            serviceChargeStatus: data.serviceChargeStatus
                        })
                    },
                    end: function (i) {
                        $("#editForm")[0].reset();
                        $("#chargeDiv").hide();
                        layer.close(i)
                        chargeId = null
                    }
                });
            } else if (obj.event === "del") {
                confirm('确定要删除该手续费吗？', function () {
                    postAjaxReq("serviceCharge/remove", {serviceChargeId: chargeId}, function (res) {
                        if (res.code == 1) {
                            layer.msg("删除成功")
                            table.reload('chargeList')
                        } else {
                            layer.msg(res.msg)
                        }
                    });
                    chargeId = null;
                })
            } else if (obj.event === "locations") {
                layer.open({
                    type: 1,
                    title: "手续费信息",
                    offset: [offsetH],
                    area: ['600px', '500px'],
                    content: $("#locationDiv"),
                    success: function () {
                        postAjaxReq("location/listByChargeId", {chargeId: chargeId},
                            function (result) {
                                if (result.code) {
                                    initTree(result.data)
                                }
                            }
                        )
                    },
                    end: function (i) {
                        layer.close(i)
                        $("#locationDiv").hide()
                        chargeId = null
                        locationIds = null
                        $("#locationTree").jstree('destroy')
                    }
                });
            }
        });
        $("#addCharge").on("click", function () {
            layer.open({
                type: 1,
                title: "新增手续费",
                offset: [offsetH],
                area: ['600px', '430px'],
                content: $("#chargeDiv"),
                end: function (i) {
                    $("#editForm")[0].reset();
                    $("#chargeDiv").hide();
                    layer.close(i)
                }
            });
        })

        form.on('submit(editSubmit)', function (obj) {
            var url = null;
            var data = obj.field
            if (!isEmpty(chargeId)) {
                data.serviceChargeId = chargeId
                url = "serviceCharge/edit"
            } else
                url = "serviceCharge/save"
            postAjaxReq(url, data, function (res) {
                if (res.code == 1) {
                    layer.closeAll();
                    table.reload('chargeList')
                    chargeId = null
                    layer.msg("保存成功")
                } else {
                    layer.msg(res.msg)
                }
            })

        });
        form.on('submit(locationFormSubmit)', function (obj) {
            if (locationIds == null){

            }
            if (locationIds)
                locationIds.forEach(function (e) {
                    e["chargeId"] = chargeId
                })
            if (locationIds == null || locationIds.length == 0)
                locationIds.push({
                    chargeId: chargeId
                })
            jsonAjaxReq("serviceCharge/location/save", locationIds, function (res) {
                if (res.code == 1) {
                    $("#locationCancel").click();
                    layer.msg("操作成功")
                    chargeId = null
                } else {
                    layer.msg(res.msg)
                }
            })
        });

        $("#search").on("click", function () {
            var chargeName = $("#chargeName").val(),
                serviceChargeStatus = $("#serviceChargeStatus").val();
            if (serviceChargeStatus == -1) serviceChargeStatus = null
            table.reload('chargeList', {
                page: {curr: 1},
                where: {
                    chargeName: chargeName,
                    serviceChargeStatus: serviceChargeStatus
                }
            })
        });
        $("#cancel").on("click", function () {
            $("#editForm")[0].reset();
            $("#chargeDiv").hide();
            layer.closeAll()
        });
    });
})
$("#locationCancel").on("click", function () {
    $("#locationDiv").hide()
    layer.closeAll()

});
